package ssi.actions;

import java.io.File;
import java.io.FileOutputStream;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import org.apache.struts.action.Action;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;

import org.apache.struts.upload.FormFile;

import ssi.beans.Animal;

import ssi.tools.Connector;


public class EditAnimalAction extends Action {
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		request.setCharacterEncoding("UTF-8");
		String target=null;
		Animal animal=(Animal)form;
		int id = Integer.parseInt(animal.getId());
		String birthDate=animal.getBirthDate();
		String gender=animal.getGender();
		String breed=animal.getBreed();
		String name=animal.getName();
		FormFile file = animal.getFile();
		ActionErrors actionErrors = new ActionErrors();
		
		String filePath = "D:/Dropbox/Studia/Semestr VII/Sieciowe systemy informatyczne/Moje/Projekt/SSIProject/SSIProject/WebContent/images/animals";
		File folder = new File(filePath);
		if (!folder.exists()) {
			folder.mkdir();
		}

		String fileName = file.getFileName();
		try {
			if (!(name.equals("") || gender.equals("") || breed.equals("")|| birthDate.equals(""))) {
				

					System.out.println("FOlder:" + filePath);
					File newFile = new File(filePath, fileName);

					if (!newFile.exists()) {
						FileOutputStream fos = new FileOutputStream(newFile);
						fos.write(file.getFileData());
						fos.flush();
						fos.close();
					}

					System.out.println(name + " " + gender + " " + breed + " "
							+ birthDate + " " + newFile);
					Connection connection = new Connector().getConnection();
					Statement statement = connection.createStatement();
					statement.setMaxRows(1);
					ResultSet result = statement.executeQuery("SELECT birthDate, gender, breed, name, images FROM `animals` WHERE id=\""+ id + "\"");
    				statement.executeUpdate("Update animals set birthDate=\""+birthDate+"\", gender=\""+gender+"\", breed=\""+breed+"\", name=\""+name+"\", images=\""+fileName+"\" WHERE id='"+id+"'");
    				target="success";
    				actionErrors.add("info",
							new ActionMessage("error.animalChanged"));
					result.close();
					connection.close();
    			}else{
    			//puste pola
    			actionErrors.add("info",
						new ActionMessage("error.emptyFields"));
    			target="failure";
    		}
		}catch(Exception e){
			actionErrors.add("info",
					new ActionMessage("error.catch"));
		}
		
		if(actionErrors.size() !=0){
			extracted(request, actionErrors);
			return new ActionForward(mapping.getInput());
		}
	
		
		return mapping.findForward(target);
	}
	
	
	private void extracted(HttpServletRequest request, ActionErrors actionErrors) {
		saveErrors(request, actionErrors);
	}


}
